/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 065.c
 * Backtrace for Exhausting
 * this program provides a method to traversal without recursion
 ***********************************************************************************************/

#include <stdio.h>

/*
 *
 */

#define NUM 5
#define C_NUM 4

int a[NUM+1];
int b[] = {0, 2, 3, 5, 7};

int main()
{
	int i = 0, not_finish = 1;
	i = 2;
	a[1] = 1;

	while (not_finish) {
		while (not_finish && i <= NUM)
			// backtrace happens when a[5] is exhausted (i=NUM=5);
			if (a[i] == C_NUM)
			// a[i] is exhausted
				if (i == 1 && a[1] == C_NUM)
				// a[1] is exhausted, FIN
					not_finish = 0;
				else
				// a[i] is exhausted, clear it and backtrace to a[i-1]
				// this clear will continues until an un-exhausted a[j]
				// which means a{012555} -> a{012000}
					a[i--] = 0;
			else
			// or just try more (a[i]++)
			// which means a{012000} -> a{013000} -> a{013100} -> a{013110} -> a{013111}
				a[i++]++;
		if (not_finish)
		{
			printf("%d%d%d, %d%d\n", b[a[1]], b[a[2]], b[a[3]], b[a[4]], b[a[5]]);
			i = NUM;
		}
	}
}

